// pages/address/editAddress.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //array: ['美国', '中国', '巴西', '日本'],
    index: 0,
    // province: "省份",
    // city: "城市",
    // town: "区县",
    updateFlag: false,// 编辑地址标识
    region: ['省份', '城市', '区县'],
    type: "", //newAddeess 新增地址  editAddeess 修改地址
    name: '',
    phone: "",
    address: "",
    id: 0, //修改地址的id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    let _this = this;
    console.log(options)
    if (options.type == 'newAddeess'){ //新增地址
      console.log(11)
      _this.setData({
        type: "newAddeess"
      })
    } else if (options.type == 'editAddeess'){ //修改地址
      _this.setData({
        type: "editAddeess",
        id: options.id
      })
      _this.queryAddressId();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.globalData.otherPageHideFlag = true;
  },
  /**
   * 选择省市区
   * 
   */
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  //保存地址
  saveAddress(){
    let _this = this;
    let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (_this.data.name == "" || _this.data.phone == "" || _this.data.address == "" || _this.data.region[0] == "省份") {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!myreg.test(_this.data.phone)) {
      wx.showToast({
        title: '手机号码格式不对',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (_this.data.type == "newAddeess"){ //新建操作
      _this.newAddress()
    }else{
      _this.eidtAddress()
    }
  },

  //新建地址
  newAddress(){
    let _this = this;
    let data = {
      "name": _this.data.name,
      "telephone": _this.data.phone,
      "province": _this.data.region[0],
      "city": _this.data.region[1],
      "town": _this.data.region[2],
      "detailAddress": _this.data.address,
    }
    //console.log(data)
    Api.newAddress(data).then( res => {
      //console.log(res)
      let pages = getCurrentPages(); // 获取页面栈
      let currPage = pages[pages.length - 1]; // 当前页面
      let prevPage = pages[pages.length - 2]; // 上一个页面
      prevPage.changeAddress();
      setTimeout(function(){
        wx.navigateBack({
          delta: 1
        })
      },1000)
    })
  },

  //获取姓名
  getName(e) {
    let _this = this;
    _this.setData({
      name: e.detail.value
    })
  },

  //获取手机号
  getPhone(e) {
    let _this = this;
    _this.setData({
      phone: e.detail.value
    })
  },

  //获取地址
  getAddress(e) {
    let _this = this;
    _this.setData({
      address: e.detail.value
    })
  },

  //根据id查询地址信息
  queryAddressId(){
    let _this  = this;
    Api.queryAddressId(_this.data.id).then( res =>{
      console.log(res)
      let arr = [res.data.province, res.data.city, res.data.town]
      _this.setData({
        name: res.data.name,
        phone: res.data.telephone,
        address: res.data.detailAddress,
        region: arr
      })
    })
  },

  //修改地址成功
  eidtAddress(){
    let _this = this;
    let data = {
      "id": _this.data.id,
      "name": _this.data.name,
      "telephone": _this.data.phone,
      "province": _this.data.region[0],
      "city": _this.data.region[1],
      "town": _this.data.region[2],
      "detailAddress": _this.data.address,
    }
    //console.log(data)
    Api.eidtAddress(data).then(res => {
      //console.log(res)
      let pages = getCurrentPages(); // 获取页面栈
      let currPage = pages[pages.length - 1]; // 当前页面
      let prevPage = pages[pages.length - 2]; // 上一个页面
      prevPage.changeAddress();
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        })
      }, 1000)
    })
  }

})